package com.df.my_net_game.game_rpc.config;

import com.df.my_net_game.common.config.ConditionalOnProfile;
import com.df.my_net_game.common.config.GameDocServer;
import com.df.my_net_game.game_rpc.service.annotation.LogicController;
import com.df.my_net_game.common.utils.LogicMethodMgr;
import com.df.my_net_game.game_rpc.service.config.LogicServiceProperties;
import com.df.my_net_game.game_rpc.service.config.ServerCfg;
import com.df.my_net_game.net.GServer;
import com.df.my_net_game.net.base.GTcpServer;
import com.df.my_net_game.net.tcp.EzGameTcpDefaultServiceCb;
import com.df.my_net_game.net.tcp.EzGameTcpDefaultMsgHandler;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 作为服务提供端的配置
 * 1.收集logichandler
 * 2.路由配置处理
 * 3.doc处理
 */
public class EZGameServiceConfig implements ApplicationContextAware {
    ApplicationContext applicationContext = null;

    @Autowired
    LogicServiceProperties serviceProperties;


    //开启http服务器
    //收集请求信息
    //提供请求和响应的proto定义生成
    @ConditionalOnProfile
    @Bean
    public GameDocServer gameDocServer() {
        System.out.println(3);
        //TODO 文档服务
        //判定当前环境是否支持文档
        //判定环境
        GameDocServer gameDocServer = new GameDocServer();
        gameDocServer.start();
        return gameDocServer;
    }
    //收集controller
    @PostConstruct
    public void collectController(){
        if(serviceProperties.getControllerBaseTimes()!=null){
            LogicMethodMgr.setControllerBaseTimes(serviceProperties.getControllerBaseTimes());
        }

        Map<String, Object> beansWithAnnotation = this.applicationContext.getBeansWithAnnotation(LogicController.class);
        if(beansWithAnnotation == null || beansWithAnnotation.isEmpty()){
            return;
        }
        Collection<Object> controllers = beansWithAnnotation.values();
        for (Object controller : controllers) {
            LogicMethodMgr.parseControllerObj(controller);
        }

        //TODO 启动当前节点的服务

        //TODO 启动对外的服务器(客户端连接的服务，如果rpc服务和客户端服务同类型和端口，则起一个就行了)
        //启动RPC服务器
        //收到消息后
        GTcpServer server = GServer.tcpServer();
        //TODO 这个回调应该从容器中拿，可以让用户写
        EzGameTcpDefaultServiceCb cb = new EzGameTcpDefaultServiceCb();
        server.setServerCB(cb);
        ServerCfg rpcServer = serviceProperties.getRpcServer();
        if(rpcServer==null){
            throw new RuntimeException("ezgame rpc服务未配置！rpc服务启动失败！");
        }
        String type = rpcServer.getType();
        type = type.trim().toLowerCase();
        if(!StringUtils.hasLength(type) ||!type.equals("tcp")){
            type="tcp";
//            throw new RuntimeException("ezgame rpct的类型只支持 tcp 和 websocket，当前配置类型为:"+type);
        }
//        if(serviceProperties.getPort()!=null){
//            server.setPort(serviceProperties.getPort());
//        }
        server.setPort(rpcServer.getPort());
        server.addHandler(()->new EzGameTcpDefaultMsgHandler(server));

        server.start();
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
